<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class ArticleController extends BaseController
{
    protected $articleModel;

    public function __construct()
    {
        parent::__construct();
        $this->articleModel = new \app\api\model\ArticleModel();
    }

    /**
     * 文章列表
     * @param int       $toolid
     * @param string    $userid 
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Article');
                $check = $validate->scene('index')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                $pagesize = !empty($data['pagesize']) ? $data['pagesize'] : 10;
                $page     = !empty($data['page']) ? $data['page'] : 1;
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->articleModel->list($data['toolid'],$this->userid,$pagesize,$page));            
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }   
    }

    /**
     * 编辑文章
     * @param int       $articleid 
     * @param string    $content 
     * @param string    $userid
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Article');
                $check = $validate->scene('edit')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                if($this->articleModel->editArticle($this->userid,$data['articleid'],$data['content']) === true){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);    
                }
                return $this->jsonJump(ApiErrCode::EDIT_ARTICLE_FAIL,ApiErrMsg::EDIT_ARTICLE_FAIL);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 删除文章
     * @param int       $articleid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Article');
                $check = $validate->scene('delete')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                if($this->articleModel->deleteArticle($this->userid,$data['articleid']) === true){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);    
                }
                return $this->jsonJump(ApiErrCode::DELETE_ARTICLE_FAIL,ApiErrMsg::DELETE_ARTICLE_FAIL);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }
}